package com.suke.money.Web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.money.suke.utils.Constants;
import com.money.suke.utils.Result;
import com.suke.money.model.LoanInfo;
import com.suke.money.model.User;
import com.suke.money.service.BidInfoService;
import com.suke.money.service.LoanInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: liush
 * @CreateTime: 2022-12-27  19:03
 * @Description: 投资控制层
 */
@Controller
public class BidController {

    @Reference(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 20000)
    LoanInfoService loanInfoService;


    @Reference(interfaceClass = BidInfoService.class, version = "1.0.0", timeout = 20000)
    BidInfoService bidInfoService;

    @RequestMapping("/loan/bidInfo/invest")
    @ResponseBody
    public Object invest(@RequestParam(name = "bidMoney", required = true) Double bidMoney,
                         @RequestParam(name = "loanId", required = true) Integer loanId,
                         HttpServletRequest request) {

        //是否登录
        User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
        if (!ObjectUtils.allNotNull(user)) {
            return Result.error("请先登录后，再投资");
        }

        //基础验证
        LoanInfo loanInfo = loanInfoService.queryLoanInfoById(loanId);

        //投资
        Map<String, Object> parasMap = new HashMap<>();
        parasMap.put("bidMoney", bidMoney);
        parasMap.put("loanId", loanId);
        parasMap.put("userId", user.getId());

        //模拟千人并发投资
        /*ExecutorService executorService = Executors.newFixedThreadPool(16);
        for (int i = 0; i < 1000; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    bidInfoService.invest(parasMap);
                }
            });
        }*/

        Integer result = bidInfoService.invest(parasMap);
        if (result.intValue() == Constants.BID_LOAN_MONEY_NOT_ENOUGH) {
            return Result.error("您好，剩余可投金额不足");
        }
        if (result.intValue() == Constants.BID_LOAN_MONEY_FAIL) {
            return Result.error("您好，您的投资超过了剩余可投金额");
        }
        if (result.intValue() == Constants.BID_LOAN_STATE_FAIL) {
            return Result.error("您好，产品状态更新失败");
        }
        if (result.intValue() == Constants.BID_FINANCE_MONEY_FAIL) {
            return Result.error("您好，您的余额不足，请充值");
        }
        if (result.intValue() == Constants.BID_INSERT_FAIL) {
            return Result.error("您好，您的投资生成失败");
        }

        return Result.success();
    }
}
